var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const screenshot = require('screenshot-desktop');

app.get('/', function (req, res) {
    screenshot().then((img) => {
        var base64Img = img.toString('base64');  // base64图片编码字符串
        // console.log(base64Img);
        const src = "data:image/png;base64," + base64Img;
        const html = `<html>
        <head>
            <style>
                .show{
                    height: 600px;
                    width: 800px;
                }
            </style>
        </head>
        <body>
            <img class="show" src="${src}" alt=""  />
        </body>
    </html>`;
        res.send(html);
    }).catch((err) => {
        // ...
        console.log(err)
    })
});

io.on('connection', function (socket) {
    //接收数据
    socket.on('login', function (obj) {
        console.log(obj.username);
        // 发送数据
        socket.emit('relogin', {
            msg: `你好${obj.username}`,
            code: 200
        });
    });
    socket.on('getShareScreen', function (someMsg) {
        console.log(someMsg);
        screenshot().then((img) => {
            // 发送数据
            socket.emit('sendShareScreen', img);
        }).catch((err) => {
            // ...
            console.log(err)
        })


    });

});

http.listen(4000, function () {
    console.log('listening on *:4000');
});